<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'

const animationDuration = 1000

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '450px'
    },
    chartData: {
      type: Object,
      required: true
    },
    chartSet: {
      type: Object,
      default: ()=>{}
    },
    chartType: {
      type: Array,
      required: true
    },
  },
  data() {
    return {
      chart: null,
      seriesData: null,
      series:[
        {
          //name: 'pageA',
          type: 'bar',
          //数据堆叠
          //stack: 'vistors',
          barWidth: '20%',
          animationDuration ,
          itemStyle:{
            normal:{
              color:function(params){
                //console.log(params)
                var index_num = params.data.profit_difference;
                if(index_num>30){
                  return "#ff4242"
                }else if(index_num<=30&&index_num>15){
                  return "#ff7c07"
                }else if(index_num<=15&&index_num>5){
                  return "#fdc927"
                }else{
                  return "#93ed39"
                }
              }
            }
          },
        },
        {
          // name: 'pageB',
          type: 'line',
          itemStyle: {
            normal: {
              color: '#3282db',
              lineStyle: {
                color: '#3282db',
                width: 2
              },
            }
          },
          yAxisIndex: 1,
          smooth: false,
          // stack: 'vistors',
          // barWidth: '60%',
          // data: [80, 52, 200, 334, 390, 330, 220],
          animationDuration
        }        
      ]
    }
  },
  watch:{
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val,this.chartSet)
      }
    },
  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    setOptions(dataset,{xName,yName}={}){
      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow', // 默认为直线，可选为：'line' | 'shadow'
            axis: 'x',
          },
          formatter: (params)=> {
            //console.log(params)
            let res='<div class=tooltipStyle><div class=list><div class=listTitle>'+params[0].data.begin_time+'至'+params[0].data.end_time+'</div><div>'+params[0].data.area_name+' - '+params[0].data.project_type+' - '+params[0].data.no+' '+'ID '+params[0].data.project_id+'</div><div>盈利率：'+params[0].data.profit+'%'+(params[0].data.profit_difference>5?"（异常）":"")+'</div><div>偏差率：'+params[0].data.profit_difference+'%'+'</div><div>收币数：'+params[0].data.coins+'</div><div>收币金额：'+params[0].data.money+'</div></div></div>';
            return res;
          },
          padding: [5, 10]
        },
        grid: {
          // top: 10,
          // left: '2%',
          // right: '2%',
          // bottom: '3%',
          left: 30,
          right: 20,
          bottom: '10%',
          top: 50,
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          name:xName?xName:"",
          nameLocation:"center",
          nameGap:30,
          splitLine:{show: false},
          //data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [{
          type: 'value',
          name:yName?yName:"",
          nameTextStyle:{
            padding:[0,0,0,20]
          },
          splitLine:{show: false},
          splitArea:{show: false},
          // axisLabel:{
          //   formatter: '{value}%'
          // },
          axisTick: {
            show: false
          }
        },{
          type: 'value',
          splitLine:{show: false},
          splitArea:{show: false},
          axisTick: {
            show: false
          }
        }],
        dataset:dataset,
        series:this.seriesData,
        // series: [{
        //   //name: 'pageA',
        //   type: 'bar',
        //   //数据堆叠
        //   //stack: 'vistors',
        //   barWidth: '20%',
        //   animationDuration ,
        //   itemStyle:{
        //     normal:{
        //       color:function(params){
        //         //console.log(params)
        //         var index_num = params.data.time;
        //         if(index_num>200){
        //           return "#ff4242"
        //         }else if(index_num<=200&&index_num>100){
        //           return "#ff8f2b"
        //         }else if(index_num<=100&&index_num>50){
        //           return "#fdc927"
        //         }else{
        //           return "#93ed39"
        //         }
        //       }
        //     }
        //   },
        // }, 
        // {
        //   // name: 'pageB',
        //   type: 'line',
        //   itemStyle: {
        //     normal: {
        //       color: '#3282db',
        //       lineStyle: {
        //         color: '#3282db',
        //         width: 2
        //       },
        //     }
        //   },
        //   yAxisIndex: 1,
        //   smooth: false,
        //   // stack: 'vistors',
        //   // barWidth: '60%',
        //   // data: [80, 52, 200, 334, 390, 330, 220],
        //   animationDuration
        // }
        // , {
        //   type: 'line',
        //   itemStyle: {
        //     normal: {
        //       color: '#ffa216',
        //       lineStyle: {
        //         color: '#ffa216',
        //         width: 2
        //       },
        //     }
        //   },
        //   yAxisIndex: 1,
        //   smooth: false,
        //   animationDuration
        // }
        // ]
      })
    },
    initChart() {
      this.seriesData=[];
      for(let val of this.chartType){
        if(val.type=="bar"){
          this.seriesData.push(this.series[0])
        }else if(val.type=="line"){
          let obj=JSON.parse(JSON.stringify(this.series[1]))
          if(val.color){
            obj.itemStyle.normal.color=val.color;
            obj.itemStyle.normal.lineStyle.color=val.color;
          }
          this.seriesData.push(obj);
        }
      }
      //console.log(this.seriesData)
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData,this.chartSet);
    },
  }
}
</script>
